<?php
if(isset($_POST['btnLogin']))
{
	$MaKH = $_POST['MaKH'];
	$MatKhau = $_POST['MatKhau'];
	
	$sql = "SELECT * FROM KhachHang WHERE MaKH='$MaKH'";
	$rows = mysql_query($sql);
	if($row = mysql_fetch_array($rows))
	{
		if($row['HieuLuc'] == 0)
		{
			$errLogin = "Not activated yet !";
		}
		
		else if($row['MatKhau'] == $MatKhau)
		{
			// luu thanh vien vao sesion
			$_SESSION['KhachHang'] = $row;
			$_SESSION[$MaKH.'_SoLanDangNhap'] = 0;
			
			// ghi nho tai khoan vao cookie
			if(isset($_POST['chkGhiNhoTK'])) 
			{
				// co check=>luu 1 thang
				$TuoiTho = time() + 30*24*60*60;
			}
			else
			{
				// khong check=>huy cookie
				$TuoiTho = time() - 30*24*60*60;
			}
			setcookie("MaKH", $MaKH, $TuoiTho);
			setcookie("MatKhau", $MatKhau, $TuoiTho);

			// chuyen ve trang yeu cau truoc do
			if(isset($_SESSION['RequestUrl']))
			{
				header("location: " . $_SESSION['RequestUrl']);
			}
			else
			{
				header("location: index.php");
			}
		}
		else
		{
			$errLogin = "Invalid member password !";
			
			if(isset($_SESSION[$MaKH.'SoLanDangNhap']))
			{
				$_SESSION[$MaKH.'SoLanDangNhap'] += 1;
			}
			else
			{
				$_SESSION[$MaKH.'SoLanDangNhap'] = 1;
			}
		}
	}
	else
	{
		$errLogin = "Invalid member id !";
	}
}
else if($_GET['btnSignOut'] == "1")
{
	unset($_SESSION['KhachHang']);
}
?>

<?php
if(!isset($_SESSION['KhachHang']))
{
?>
<form method="post">
<table cellpadding="0" cellspacing="0" style="width:90%;">
    <tr>
        <td colspan="2">
            User Name:</td>
    </tr>
    <tr>
        <td colspan="2">
            <input name="MaKH" style="width:99%" value="<?php echo $_COOKIE['MaKH']?>"/>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            Password:</td>
    </tr>
    <tr>
        <td colspan="2">
            <input name="MatKhau" style="width:99%" value="<?php echo $_COOKIE['MatKhau']?>"/>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <input name="chkGhiNhoTK" type="checkbox"  <?php echo isset($_COOKIE['MaKH']) ? 'checked' : ''?>/>Keep me for next?
        </td>
    </tr>
    <tr>
        <td colspan="2" align="right">
            <?php echo isset($errLogin) ? $errLogin : 'Please login to purchase !'?>
        </td>
    </tr>
    <tr>
        <td align="right" colspan="2">
        	<input type="submit" name="btnLogin" value="Login" class="art-button"/>
        </td>
    </tr>
    <tr>
        <td>
            <img src="Hinh/icons/User.png" />
        </td>
        <td>
        	<a href="?p=SignUp">Register new user?</a>
        </td>
    </tr>
    <tr>
        <td>
            <img src="Hinh/icons/Problem.png" />
        </td>
        <td>
            <a href="?p=ForgotPassword">Forgot password?</a>
        </td>
    </tr>
</table>
</form>
<?php
}
else
{
?>
<table cellpadding="5" cellspacing="0" style="width:90%;">
    <tr>
        <td align="center">
            <img src="Hinh/KhachHang/<?php echo $_SESSION['KhachHang']['Hinh']?>" Width="145px" />
        </td>
    </tr>
    <tr>
        <td align="center">
            <a href="?p=EditProfile"><?php echo $_SESSION['KhachHang']['HoTen']?></a>
        </td>
    </tr>
</table>
<fieldset>
	<legend>Personal functions</legend>
<table cellpadding="0" cellspacing="0" style="width:90%">
    <tr>
        <td>
            <table cellpadding="2" cellspacing="0">
                
                <tr>
                    <td>
                        <img src="Hinh/icons/Unlock.png" />
                    </td>
                    <td>
                        <a href="?btnSignOut=1">Sign out</a>
                    </td>
                </tr>
                <tr>
                    <td>
                        <img src="Hinh/icons/Open%20folder.png" />
                    </td>
                    <td>
                        <a href="?p=PurchasedOrders">Purchased orders</a>
                    </td>
                </tr>
                <tr>
                    <td>
                        <img src="Hinh/icons/Bluetooth.png" />
                    </td>
                    <td>
                        <a href="?p=ProductList&List=PURCHASED">Purchased products</a>
                    </td>
                </tr>
                
                <tr>
                    <td>
                        <img src="Hinh/icons/Open%20letter.png" alt="" />
                    </td>
                    <td>
                        <a href="?p=ProductList&List=SENT">Sent products</a>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>
</fieldset>
<?php
}
?>
